#include <iostream>
#include <string>
using namespace std;

int main()
{
    int T = 0, n = 0, k = 0;
    string str;

    cin >> T;
    while (T--)
    {
        int cnt = 0, ret = 0;
        cin >> n >> k >> str;
        for (int i = 0; i < n; ++i)
        {
            if (str[i] == 'W') {
                ++ret;
                ++cnt;
                if (cnt % 3 == 0)
                {
                    cnt = 2;
                    ret += (k - 1);
                }
            }
            else {
                --ret;
                cnt = 0;
            }
        }
        cout << ret << endl;
    }

    return 0;
}